package com.chaodev.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.dao.ActivityDivisionDao;
import com.chaodev.base.dao.AttDao;
import com.chaodev.base.dto.ActivityDivisionDto;
import com.chaodev.base.em.BizTypeEnum;
import com.chaodev.base.model.ActivityDivision;
import com.chaodev.base.model.ActivityNotice;
import com.chaodev.base.model.Att;
import com.chaodev.base.model.Volunteer;
import com.chaodev.base.service.ActivityDivisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ActivityDivisionServiceImpl implements ActivityDivisionService {

    @Autowired
    private ActivityDivisionDao activityDivisionDao;

    @Autowired
    private AttDao attDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addActivityDivision(ActivityDivision activityDivision) {
        if (activityDivision.getDivisionDetail() != null && activityDivision.getDivisionDetail().size() > 0) {
            int flag = 0;
            ActivityDivision insert = new ActivityDivision();
            insert.setActivityId(activityDivision.getActivityId());
            insert.setCreateUser(activityDivision.getCreateUser());
            for (ActivityDivision a : activityDivision.getDivisionDetail()) {
                insert.setDivision(a.getDivision());
                String[] codes = a.getVolunteerUserCode() == null ? new String[0] : a.getVolunteerUserCode().split(",");
                String[] names = a.getVolunteerUserName() == null ? new String[0] : a.getVolunteerUserName().split(",");
                for (int i = 0; i < codes.length; i++) {
                    insert.setVolunteerUserCode(codes[i]);
                    insert.setVolunteerUserName(names[i]);
                    flag += activityDivisionDao.insertActivityDivision(insert);
                }
            }
            return flag != 0;
        }
        return activityDivisionDao.insertActivityDivision(activityDivision) != 0;
    }

    @Override
    public boolean editActivityDivision(ActivityDivision activityDivision) {
        return activityDivisionDao.updateActivityDivision(activityDivision) != 0;
    }

    @Override
    public boolean removeActivityDivision(ActivityDivision activityDivision) {
        return activityDivisionDao.deleteActivityDivision(activityDivision.getId()) != 0;
    }

    @Override
    public List<ActivityDivision> getActivityDivisionList(ActivityDivisionDto activityDivisionDto) {
        return activityDivisionDao.selectActivityDivisionList(activityDivisionDto);
    }

    @Override
    public PageInfo<ActivityDivision> getActivityDivisionPage(ActivityDivisionDto activityDivisionDto) {
        PageHelper.startPage(activityDivisionDto.getPage(), activityDivisionDto.getRows());
        List<ActivityDivision> list = activityDivisionDao.selectActivityDivisionList(activityDivisionDto);
        PageInfo<ActivityDivision> pageInfo = new PageInfo<>(list, activityDivisionDto.getRows());
        return pageInfo;
    }

    @Override
    public List<Volunteer> getVolunteerByActivity(String id) {
        return activityDivisionDao.selectVolunteerByActivity(id);
    }

    @Override
    public PageInfo<Volunteer> getVolunteerByActivity(String id, int page, int rows) {
        PageHelper.startPage(page, rows);
        List<Volunteer> list = activityDivisionDao.selectVolunteerByActivity(id);
        return new PageInfo<>(list, rows);
    }

    @Override
    public PageInfo<ActivityNotice> getActivityNoticeNotDivisionPage(ActivityDivisionDto activityDivisionDto) {
        PageHelper.startPage(activityDivisionDto.getPage(), activityDivisionDto.getRows());
        List<ActivityNotice> list = activityDivisionDao.selectActivityNoticeNotDivision(activityDivisionDto.getVolunteerUserCode());
        Att att = null;
        List<Att> attList;
        for (int i = 0; i < list.size(); i++) {
            att = new Att();
            att.setDataNo(list.get(i).getId());
            att.setState(1);
            att.setBizType(BizTypeEnum.Em_Notice.getValue());
            attList = attDao.queryDataList(att);
            list.get(i).setAtts(attList.stream().filter(d -> "fm".equals(d.getAddType())).collect(Collectors.toList()));
            list.get(i).setAnnex(attList.stream().filter(d -> "fj".equals(d.getAddType())).collect(Collectors.toList()));
        }
        PageInfo<ActivityNotice> pageInfo = new PageInfo<>(list, activityDivisionDto.getRows());
        return pageInfo;
    }
}
